/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * ChatDialog.java
 *
 * Created on Mar 4, 2010, 5:43:53 AM
 */

package FICS;

import TelnetClient.TelnetInterface;
import bbchess.MainGui;
import java.awt.Point;

/**
 * Used to gather info for new match posting
 */
public class MatchDialog extends javax.swing.JDialog {
    
    private TelnetInterface telnetInterface;
    java.awt.Frame parent;

    /** Creates new form ChatDialog */
    public MatchDialog(java.awt.Frame parent, boolean modal, TelnetInterface telnetInterface) {
        super(parent, modal);
        initComponents();
        this.parent = parent;
        this.telnetInterface = telnetInterface;
        //centerFrameToGui();
        setLocationRelativeTo(null);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        playerNameField = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        requestMatchButton = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Request Match");
        setResizable(false);

        jLabel1.setText("Player to Challenge:");

        playerNameField.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                playerNameFieldKeyPressed(evt);
            }
        });

        jLabel2.setText("Challenge a player to a match.");

        requestMatchButton.setText("Request Match");
        requestMatchButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                requestMatchButtonActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel2)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(playerNameField, javax.swing.GroupLayout.DEFAULT_SIZE, 74, Short.MAX_VALUE))
                    .addComponent(requestMatchButton, javax.swing.GroupLayout.Alignment.TRAILING))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel2)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(playerNameField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 22, Short.MAX_VALUE)
                .addComponent(requestMatchButton)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void requestMatchButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_requestMatchButtonActionPerformed
        requestMatch();
    }//GEN-LAST:event_requestMatchButtonActionPerformed

    private void centerFrameToGui() {
        MainGui main = (MainGui) parent;
        Point mainCenter = main.getCenter();
        setLocation(new Point((int) mainCenter.getX() - getWidth()/2, (int) mainCenter.getY() - getHeight()/2));
    }

    private void requestMatch() {
        telnetInterface.tellTelnet("match " + playerNameField.getText());
        dispose();
    }

    private void playerNameFieldKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_playerNameFieldKeyPressed
        if (evt.getKeyCode() == evt.VK_ENTER) {
            requestMatch();
        }
    }//GEN-LAST:event_playerNameFieldKeyPressed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JTextField playerNameField;
    private javax.swing.JButton requestMatchButton;
    // End of variables declaration//GEN-END:variables

}
